<!doctype html>
<html lang="zh-CN">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
    <link rel="stylesheet" href="/js/prism.css">
    <link rel="stylesheet" href="/css/app.css">
    <title>Json5 语法说明 - 叫兽</title>
    <link rel="icon" type="image/x-icon" class="js-site-favicon" href="/img/favicon.ico">
  </head>
  <body>

        <div class="container bg-white pb-5 px-5 spx-0">
            
            <h1 class="post-title">Json5 语法说明</h1>




            <p>在开发Panda Api的最初版本，文档是用json来写的，json语法不支持注释，不支持字符串换行，所有的field都必须双引号，末尾不能有多余的逗号...等等，一大堆极其严格的要求和不支持的功能。</p><p>一开始，我就自己增加了json编译器，来对json语法进行拓展，直到发现了<code>json5</code>，这完全就是我想要的！</p><p><code>JSON5</code>是对<code>JSON</code>的扩展，让人可以更容易手工编写和维护。</p><p><code>JSON5</code>的特性如下：</p><h3>对象 Objects</h3><ul><li>key值允许没有双引号 Object keys may be an ECMAScript 5.1 IdentifierName.<pre><code class=".language-json5">{
    code:1,
    msg:&quot;Hello&quot;
}
</code></pre></li><li>允许有多余的逗号结尾 Objects may have a single trailing comma.<pre><code class=".language-json5">{
    code:1,
    msg:&quot;Hello&quot;,
}
</code></pre></li><li>key值可以使用单引号来包裹 Arrays may have a single trailing comma.<pre><code class=".language-json5">{
    '-name':&quot;HellO&quot;,
    code:1,
    msg:&quot;Hello&quot;,
}
</code></pre></li></ul><h3>数组 Arrays</h3><ul><li>允许有多余的逗号结尾<pre><code class=".language-json5">&#91;1, 2, 3,&#93;
</code></pre></li></ul><h3>字符串 Strings</h3><ul><li>允许使用单引号包裹字符串 Strings may be single quoted.<pre><code class=".language-json5">{
    '-name':'HellO',
    code:1,
    msg:'Hello',
}
</code></pre></li><li>字符串可以换行，可以多行 Strings may span multiple lines by escaping new line characters<pre><code class=".language-json5">{
    '-name':'HellO',
    '-desc':&quot;hello
    world!

    haha!
    &quot;,
    code:1,
    msg:'Hello',
}
</code></pre></li><li>字符串允许使用转义字符 Strings may include character escapes.<pre><code class=".language-json5">{
    code:1,
    msg:&quot;Hello\nWorld\n!&quot;,
}
</code></pre></li></ul><h3>数字 Numbers</h3><ul><li>数字可以用十六进制表示 Numbers may be hexadecimal.<pre><code class=".language-json5">{
    code:0xFF
}
</code></pre></li><li>允许使用小数点开头或结尾的数字，例如：<code>.007</code>，<code>7.</code> Numbers may have a leading or trailing decimal point.<pre><code class=".language-json5">{
    a:.007,
    b:7.
    f:-.32e-3
}
</code></pre></li><li>数字可以使用正无穷大、负无穷大、和Nan 来表示,  Numbers may be <a href='https://ieeexplore.ieee.org/document/4610935'>IEEE 754</a> positive infinity, negative infinity, and NaN.<pre><code class=".language-json5">{
    p:Infinity,
    n:-Infinity,
    b:NaN
}
</code></pre></li><li>数字前面可以有一个正号<code>+</code> Numbers may begin with an explicit plus sign.{    a:+10}</li></ul><h3>注释 Comments</h3><ul><li>支持单行注释和多行注释 Single and multi-line comments are allowed.<pre><code class=".language-json5">{
    // code表示结果代码
    code:1
    msg:&quot;返回结果说明&quot;
    /&#42;
        json5的多行注释
        真是好爽啊，
        完全就是为Panda api而设计的
    &#42;/
}
</code></pre></li></ul><h3>允许多余的空白符 White Space</h3><ul><li>允许在任何在json5相关符号之前和之后都可能出现多余的空白符。 Additional white space characters are allowed. 支持的空白符如下：<pre><code class=".language-json5">Code Points	Description
U+0009:	Horizontal tab
U+000A:	Line feed
U+000B:	Vertical tab
U+000C:	Form feed
U+000D:	Carriage return
U+0020:	Space
U+00A0:	Non-breaking space
U+2028:	Line separator
U+2029:	Paragraph separator
U+FEFF:	Byte order mark
Unicode: Zs category	Any other character in the Space Separator Unicode category

</code></pre></li></ul><p>官方相关说明介绍：</p><p><a href='https://github.com/json5/json5'>https://github.com/json5/json5</a></p>
            <p class="text-left text-muted">2020-02-05 07:50</p>
        </div>
        <div class="container">
            <div class="row mt-4">
                <div class="col-6 text-left"></div>
                <div class="col-6 text-right"><a href="/p/2020/2/2/Panda-api接口权限配置说明/">下一篇:Panda-api接口权限配置说明</a></div>
            </div>
        </div>

        <button class="btn btn-link m-menu-toggle d-md-none fixed-top collapsed" type="button" data-toggle="collapse" data-target="#m-menu" aria-controls="m-menu" aria-expanded="false" aria-label="Toggle docs navigation"><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 30 30" width="30" height="30" focusable="false"><title>Menu</title><path stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-miterlimit="10" d="M4 7h22M4 15h22M4 23h22"></path></svg>
        </button>
        <ul class="nav flex-column collapse fixed-top site-menu d-md-block" id="m-menu">
            <li class="nav-item">
                <a class="nav-link" href="/">首页</a>
            </li>
            <li class="nav-item">
                <a class="nav-link" href="/p/list/">所有文章</a>
            </li>
            <li class="nav-item">
                <a class="nav-link" href="/p/about-me/">关于我</a>
            </li>
            <li class="nav-item">
              <a class="nav-link" href="https://github.com/arlicle">Github</a>
          </li>
        </ul>

        
    <div class="container mt-5">
        <h3>留言</h3>
        <div id="commentApp"></div>
    </div>
    <script>
        var AL_configs = {
            "post_id":"/p/2020/2/5/json5/",
            "appid":"847e226e8a46f64045d45312245a68ba1368a564"
        };
        (function() {
            var hm = document.createElement("script");
            hm.src = "https://comment.debugmyself.com/sc.js";
            var s = document.getElementsByTagName("script")[0];
            s.parentNode.insertBefore(hm, s);
        })();
    </script>


        <footer class="footer mt-5 pb-2">
        <div class="container text-center">
          
          <p><span class="text-black-50">Powered by <a href="https://github.com/arlicle/ablog">ablog</a> © 2019 叫兽</span></p>
          <p><span class="text-black-50"><a href="http://www.beian.miit.gov.cn/">滇ICP备10201832号-3</a></span></p>
          
        </div>
        </footer>
        <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js" integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49" crossorigin="anonymous"></script>
        <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js" integrity="sha384-ChfqqxuZUCnJSK3+MXmPNIyE6ZbWh2IMqE241rYiqJxyMiZ6OW/JmZQ5stwEULTy" crossorigin="anonymous"></script>
        <script src="/js/prism.js"></script>
        <script>
        $(function () {
            $('[data-toggle="tooltip"]').tooltip();
        })
        </script>
        <script type="text/x-mathjax-config">
        MathJax.Hub.Config({tex2jax: {inlineMath: [['$','$'], ['\\(','\\)']]}, displayAlign: "left",scale: 180});
        </script>
        <script type="text/javascript" async
        src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/MathJax.js?config=TeX-MML-AM_SVG" async>
        </script>
  </body>
</html>